package com.hwhhhh.mygrad;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

import com.hwhhhh.mygrad.adapter.AdminClsAdapter;
import com.hwhhhh.mygrad.dto.FreeClsDTO;
import com.hwhhhh.mygrad.fragment.AdminHomeFragment;
import com.hwhhhh.mygrad.fragment.BottomHintFragment;
import com.hwhhhh.mygrad.fragment.ClsEditFragment;
import com.hwhhhh.mygrad.fragment.ClsManageFragment;
import com.hwhhhh.mygrad.fragment.HintFragment;

public class AdminActivity extends BaseActivity implements HintFragment.HintCallBack {
    public static final int DELETE_FRAG_CODE = 201;
    public static final int EDIT_FRAG_CODE = 202;

    @Override
    protected void initView() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_admin;
    }

    @Override
    protected void initData() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.admin_container, new AdminHomeFragment(), AdminHomeFragment.class.getSimpleName())
                .commit();
    }

    public void replaceTo(Fragment fragment, String tag) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.admin_container, fragment, tag)
                .commit();
    }

    @Override
    public void doPositive(int requestCode) {
        if (requestCode == DELETE_FRAG_CODE) {   // 确定删除空闲教室
            HintFragment hintFragment = (HintFragment) getSupportFragmentManager().findFragmentByTag(HintFragment.class.getSimpleName());
            if (hintFragment != null) {
                Bundle bundle = hintFragment.getBundle();
                int position = bundle.getInt("position");
                FreeClsDTO freeClsDTO = (FreeClsDTO) bundle.getSerializable("freeCls");
                ClsManageFragment clsManageFragment = (ClsManageFragment) getSupportFragmentManager().findFragmentByTag(ClsManageFragment.class.getSimpleName());
                if (clsManageFragment != null) {
                    AdminClsAdapter clsAdapter = clsManageFragment.getAdminClsAdapter();
                    clsAdapter.remove(position);
                    clsAdapter.notifyItemRemoved(position);
                    clsAdapter.notifyItemRangeChanged(position, clsAdapter.getItemCount() - position);
                    hintFragment.dismiss();
                }
            }
        } else if (requestCode == EDIT_FRAG_CODE) {
            ClsEditFragment clsEditFragment = (ClsEditFragment) getSupportFragmentManager().findFragmentByTag(ClsEditFragment.class.getSimpleName());
            if (clsEditFragment != null) {
                boolean[] clsArr = clsEditFragment.getArguments().getBooleanArray(ClsEditFragment.DATA);
                showToast(clsArr.toString());
            }
        }
    }

    @Override
    public void doNegative(int requestCode) {
        if (requestCode == DELETE_FRAG_CODE) {   // 取消删除空闲教室
            HintFragment hintFragment = (HintFragment) getSupportFragmentManager().findFragmentByTag(HintFragment.class.getSimpleName());
            if (hintFragment != null) {
                hintFragment.dismiss();
            }
        } else if (requestCode == EDIT_FRAG_CODE) { // 编辑空教室
            ClsEditFragment clsEditFragment = (ClsEditFragment) getSupportFragmentManager().findFragmentByTag(ClsEditFragment.class.getSimpleName());
            if (clsEditFragment != null) {
                clsEditFragment.dismiss();
            }
        }
    }
}